package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsHomeNewProduct;
import cn.zcscloud.mall.service.SmsHomeNewProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页新品管理Controller
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {
    @Autowired
    private SmsHomeNewProductService homeNewProductService;

    /**
     * 添加首页新品
     * @param homeNewProductList 添加的首页新品
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody List<SmsHomeNewProduct> homeNewProductList) {
        homeNewProductService.saveBatch(homeNewProductList);
        return R.success(null);
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        homeNewProductService.removeByIds(ids);
        return R.success(null);
    }

    /**
     * 首页分页展示
     * @param productName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public R list(String productName,
                  Integer recommendStatus,
                  @RequestParam(defaultValue = "5") Integer pageSize,
                  @RequestParam(defaultValue = "1") Integer pageNum){
        LambdaQueryWrapper<SmsHomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productName != null,SmsHomeNewProduct::getProductName,productName)
                .eq(recommendStatus != null,SmsHomeNewProduct::getRecommendStatus,recommendStatus);
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        Page<SmsHomeNewProduct> iPage = homeNewProductService.page(page, queryWrapper);
        return R.success(PageRequest.init(iPage));

    }

    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R  updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus) {
        SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
        smsHomeNewProduct.setRecommendStatus(recommendStatus);
        int count = 0;
        for (Long id : ids) {
            smsHomeNewProduct.setId(id);
            homeNewProductService.updateById(smsHomeNewProduct);
            count++;
        }
        if (count > 0) {
            return R.success("");
        }
        return R.error();
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public R updateSort(@PathVariable Long id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
        smsHomeNewProduct.setId(id);
        smsHomeNewProduct.setSort(sort);
        homeNewProductService.updateById(smsHomeNewProduct);
        return R.success("");
    }
}
